{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['data_batch_1', 'data_batch_2', 'data_batch_5', 'data_batch_4', 'data_batch_3', 'batches.meta', 'readme.html', 'test_batch']\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import os\n",
    "import pickle\n",
    "import numpy as np\n",
    "import os\n",
    "CIFAR_DIR = \"/home/herb/code/data/dataset/cf10data\"\n",
    "print(os.listdir(CIFAR_DIR))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data(filename):\n",
    "    \"\"\"\n",
    "    read data from data file\n",
    "    \"\"\"\n",
    "    with open(filename,'rb') as f:\n",
    "        data = pickle.load(f,encoding='bytes')\n",
    "        return data[b'data'],data[b'labels']\n",
    "    \n",
    "class CifarData:\n",
    "    def __init__(self,filenames,need_shuffle):\n",
    "        all_data = []\n",
    "        all_labels = []\n",
    "        for filename in filenames:\n",
    "            data,labels = load_data(filename)\n",
    "            for item,label in zip(data,labels):\n",
    "                if label in [0,1]:\n",
    "                    all_data.append(item)\n",
    "                    all_labels.append(label)\n",
    "        self._data = np.vstack(all_data)\n",
    "        # 数据归一化\n",
    "        self._data = self._data/127.5 -1\n",
    "        self._labels = np.hstack(all_labels)\n",
    "        self._num_examples = self._data.shape[0]\n",
    "        self._need_shuffle = need_shuffle\n",
    "        self._indicator = 0\n",
    "        if self._need_shuffle:\n",
    "            self._shuffle_data()\n",
    "            \n",
    "    def _shuffle_data(self):\n",
    "        p = np.random.permutation(self._num_examples)\n",
    "        self._data= self._data[p]\n",
    "        self._labels = self._labels[p]\n",
    "        \n",
    "    def next_batch(self,batch_size):\n",
    "        end_indicator = self._indicator + batch_size\n",
    "        if end_indicator > self._num_examples:\n",
    "            if self._need_shuffle:\n",
    "                self._shuffle_data()\n",
    "                self._indicator = 0\n",
    "                end_indicator = batch_size\n",
    "            else:\n",
    "                raise Exception(\"error\")\n",
    "        if end_indicator > self._num_examples:\n",
    "            raise Exception(\"batch size is larger than all examples\")\n",
    "        \n",
    "        batch_data = self._data[self._indicator:end_indicator]\n",
    "        batch_lables = self._labels[self._indicator:end_indicator]\n",
    "        self._indicator = end_indicator\n",
    "        return batch_data, batch_lables\n",
    "    \n",
    "\n",
    "train_filenames = [os.path.join(CIFAR_DIR,'data_batch_%d'%i)for i in range(1,6)]\n",
    "test_filenames = [os.path.join(CIFAR_DIR,'test_batch')]\n",
    "\n",
    "train_data = CifarData(train_filenames,True)\n",
    "test_data = CifarData(test_filenames,False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 占位符\n",
    "x = tf.placeholder(tf.float32,[None,3072])\n",
    "y = tf.placeholder(tf.int64,[None])\n",
    "\n",
    "# (3072,1)\n",
    "w = tf.get_variable('w', [x.get_shape()[-1],1],initializer=tf.random_normal_initializer(0,1))\n",
    "\n",
    "#(1,)\n",
    "b = tf.get_variable('b',[1],initializer=tf.constant_initializer(0.0))\n",
    "# [None,3072] * [3072,1] = [None,1]\n",
    "y_ = tf.matmul(x, w)+b\n",
    "#[None,1]\n",
    "p_y_1 = tf.nn.sigmoid(y_)\n",
    "y_reshaped = tf.reshape(y,(-1,1))\n",
    "y_reshaped_float = tf.cast(y_reshaped, tf.float32)\n",
    "\n",
    "loss = tf.reduce_mean(tf.square(y_reshaped_float - p_y_1))\n",
    "\n",
    "predict = p_y_1>0.5\n",
    "\n",
    "correct_prediction = tf.equal(tf.cast(predict,tf.int64),y_reshaped)\n",
    "\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float64))\n",
    "\n",
    "\n",
    "with tf.name_scope('train_op'):\n",
    "    train_op = tf.train.AdamOptimizer(1e-3).minimize(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train step: 0, loss: 0.7059940695762634 acc:0.25\n",
      "---------------0.4025000000000001\n",
      "Train step: 500, loss: 0.3773106634616852 acc:0.6\n",
      "Train step: 1000, loss: 0.10000644624233246 acc:0.9\n",
      "Train step: 1500, loss: 0.24451620876789093 acc:0.75\n",
      "Train step: 2000, loss: 0.1548805981874466 acc:0.85\n",
      "Train step: 2500, loss: 0.22276198863983154 acc:0.75\n",
      "Train step: 3000, loss: 0.2756558656692505 acc:0.7\n",
      "Train step: 3500, loss: 0.199855774641037 acc:0.8\n",
      "Train step: 4000, loss: 0.24993064999580383 acc:0.75\n",
      "Train step: 4500, loss: 0.15000076591968536 acc:0.85\n",
      "Train step: 5000, loss: 0.26798778772354126 acc:0.7\n",
      "---------------0.794\n",
      "Train step: 5500, loss: 0.11474253982305527 acc:0.85\n",
      "Train step: 6000, loss: 0.09999380260705948 acc:0.9\n",
      "Train step: 6500, loss: 0.1079174056649208 acc:0.9\n",
      "Train step: 7000, loss: 0.19176822900772095 acc:0.8\n",
      "Train step: 7500, loss: 0.04999904707074165 acc:0.95\n",
      "Train step: 8000, loss: 0.143672376871109 acc:0.85\n",
      "Train step: 8500, loss: 0.2067113220691681 acc:0.8\n",
      "Train step: 9000, loss: 0.24933581054210663 acc:0.75\n",
      "Train step: 9500, loss: 0.2543104588985443 acc:0.75\n",
      "Train step: 10000, loss: 0.17360714077949524 acc:0.8\n",
      "---------------0.8080000000000002\n",
      "Train step: 10500, loss: 0.15030667185783386 acc:0.85\n",
      "Train step: 11000, loss: 0.2684083878993988 acc:0.7\n",
      "Train step: 11500, loss: 0.23064680397510529 acc:0.75\n",
      "Train step: 12000, loss: 0.24989137053489685 acc:0.75\n",
      "Train step: 12500, loss: 0.15966610610485077 acc:0.85\n",
      "Train step: 13000, loss: 0.051499128341674805 acc:0.95\n",
      "Train step: 13500, loss: 0.05000875145196915 acc:0.95\n",
      "Train step: 14000, loss: 0.09987533092498779 acc:0.9\n",
      "Train step: 14500, loss: 0.19835014641284943 acc:0.8\n",
      "Train step: 15000, loss: 0.16797864437103271 acc:0.8\n",
      "---------------0.8175\n",
      "Train step: 15500, loss: 0.1496126502752304 acc:0.85\n",
      "Train step: 16000, loss: 0.29781433939933777 acc:0.7\n",
      "Train step: 16500, loss: 0.20118455588817596 acc:0.8\n",
      "Train step: 17000, loss: 0.1999787986278534 acc:0.8\n",
      "Train step: 17500, loss: 0.15212096273899078 acc:0.85\n",
      "Train step: 18000, loss: 0.00015688655548729002 acc:1.0\n",
      "Train step: 18500, loss: 0.14996422827243805 acc:0.85\n",
      "Train step: 19000, loss: 0.15003268420696259 acc:0.85\n",
      "Train step: 19500, loss: 0.15013107657432556 acc:0.85\n",
      "Train step: 20000, loss: 0.25343939661979675 acc:0.75\n",
      "---------------0.8140000000000002\n",
      "Train step: 20500, loss: 0.20007137954235077 acc:0.8\n",
      "Train step: 21000, loss: 0.1976260095834732 acc:0.8\n",
      "Train step: 21500, loss: 0.33681702613830566 acc:0.65\n",
      "Train step: 22000, loss: 0.15012288093566895 acc:0.85\n",
      "Train step: 22500, loss: 0.27489688992500305 acc:0.7\n",
      "Train step: 23000, loss: 0.15009859204292297 acc:0.85\n",
      "Train step: 23500, loss: 0.10000000894069672 acc:0.9\n",
      "Train step: 24000, loss: 0.15182852745056152 acc:0.85\n",
      "Train step: 24500, loss: 0.10044294595718384 acc:0.9\n",
      "Train step: 25000, loss: 0.24132022261619568 acc:0.75\n",
      "---------------0.8140000000000002\n",
      "Train step: 25500, loss: 0.10110639035701752 acc:0.9\n",
      "Train step: 26000, loss: 0.05000000447034836 acc:0.95\n",
      "Train step: 26500, loss: 0.4081946313381195 acc:0.55\n",
      "Train step: 27000, loss: 0.12941166758537292 acc:0.85\n",
      "Train step: 27500, loss: 0.1471002995967865 acc:0.85\n",
      "Train step: 28000, loss: 0.10059452056884766 acc:0.9\n",
      "Train step: 28500, loss: 0.13206937909126282 acc:0.8\n",
      "Train step: 29000, loss: 0.10062120109796524 acc:0.9\n",
      "Train step: 29500, loss: 0.10060516744852066 acc:0.9\n",
      "Train step: 30000, loss: 0.20057232677936554 acc:0.8\n",
      "---------------0.8145000000000002\n",
      "Train step: 30500, loss: 0.222233846783638 acc:0.75\n",
      "Train step: 31000, loss: 0.00023545343719888479 acc:1.0\n",
      "Train step: 31500, loss: 0.14998729526996613 acc:0.85\n",
      "Train step: 32000, loss: 0.10008229315280914 acc:0.9\n",
      "Train step: 32500, loss: 0.20102174580097198 acc:0.8\n",
      "Train step: 33000, loss: 0.10078656673431396 acc:0.9\n",
      "Train step: 33500, loss: 0.24999961256980896 acc:0.75\n",
      "Train step: 34000, loss: 0.05000000074505806 acc:0.95\n",
      "Train step: 34500, loss: 0.05000762268900871 acc:0.95\n",
      "Train step: 35000, loss: 0.053039778023958206 acc:0.95\n",
      "---------------0.8185000000000001\n",
      "Train step: 35500, loss: 0.10002820193767548 acc:0.9\n",
      "Train step: 36000, loss: 0.15222756564617157 acc:0.85\n",
      "Train step: 36500, loss: 0.24999983608722687 acc:0.75\n",
      "Train step: 37000, loss: 0.15000079572200775 acc:0.85\n",
      "Train step: 37500, loss: 0.2045871764421463 acc:0.8\n",
      "Train step: 38000, loss: 0.10017330944538116 acc:0.9\n",
      "Train step: 38500, loss: 0.20038263499736786 acc:0.8\n",
      "Train step: 39000, loss: 0.15007014572620392 acc:0.85\n",
      "Train step: 39500, loss: 0.14990639686584473 acc:0.85\n",
      "Train step: 40000, loss: 0.147911936044693 acc:0.85\n",
      "---------------0.8214999999999999\n",
      "Train step: 40500, loss: 0.15023131668567657 acc:0.85\n",
      "Train step: 41000, loss: 0.05168559029698372 acc:0.95\n",
      "Train step: 41500, loss: 0.19999557733535767 acc:0.8\n",
      "Train step: 42000, loss: 0.09618614614009857 acc:0.9\n",
      "Train step: 42500, loss: 0.2000245749950409 acc:0.8\n",
      "Train step: 43000, loss: 0.20451660454273224 acc:0.8\n",
      "Train step: 43500, loss: 0.1502654105424881 acc:0.85\n",
      "Train step: 44000, loss: 0.05278163403272629 acc:0.95\n",
      "Train step: 44500, loss: 0.1999996453523636 acc:0.8\n",
      "Train step: 45000, loss: 0.12750160694122314 acc:0.85\n",
      "---------------0.8150000000000003\n",
      "Train step: 45500, loss: 0.09999971836805344 acc:0.9\n",
      "Train step: 46000, loss: 0.05004441738128662 acc:0.95\n",
      "Train step: 46500, loss: 0.14999988675117493 acc:0.85\n",
      "Train step: 47000, loss: 0.05040576308965683 acc:0.95\n",
      "Train step: 47500, loss: 0.15894246101379395 acc:0.85\n",
      "Train step: 48000, loss: 0.05995122343301773 acc:0.95\n",
      "Train step: 48500, loss: 0.10000602900981903 acc:0.9\n",
      "Train step: 49000, loss: 0.15000003576278687 acc:0.85\n",
      "Train step: 49500, loss: 0.0009857387049123645 acc:1.0\n",
      "Train step: 50000, loss: 0.3008490800857544 acc:0.7\n",
      "---------------0.8170000000000001\n",
      "Train step: 50500, loss: 0.1500076949596405 acc:0.85\n",
      "Train step: 51000, loss: 0.1519288718700409 acc:0.85\n",
      "Train step: 51500, loss: 0.1500129997730255 acc:0.85\n",
      "Train step: 52000, loss: 0.2962244153022766 acc:0.7\n",
      "Train step: 52500, loss: 0.10332449525594711 acc:0.9\n",
      "Train step: 53000, loss: 0.1503077894449234 acc:0.85\n",
      "Train step: 53500, loss: 0.1008261889219284 acc:0.9\n",
      "Train step: 54000, loss: 0.10276732593774796 acc:0.9\n",
      "Train step: 54500, loss: 0.19948513805866241 acc:0.8\n",
      "Train step: 55000, loss: 0.05138915032148361 acc:0.95\n",
      "---------------0.812\n",
      "Train step: 55500, loss: 0.25000205636024475 acc:0.75\n",
      "Train step: 56000, loss: 0.15504702925682068 acc:0.85\n",
      "Train step: 56500, loss: 0.10480514913797379 acc:0.9\n",
      "Train step: 57000, loss: 0.00024461490102112293 acc:1.0\n",
      "Train step: 57500, loss: 0.10013413429260254 acc:0.9\n",
      "Train step: 58000, loss: 0.11343225091695786 acc:0.85\n",
      "Train step: 58500, loss: 0.09999912977218628 acc:0.9\n",
      "Train step: 59000, loss: 0.10123727470636368 acc:0.9\n",
      "Train step: 59500, loss: 0.0500209704041481 acc:0.95\n",
      "Train step: 60000, loss: 0.10000000149011612 acc:0.9\n",
      "---------------0.8164999999999999\n",
      "Train step: 60500, loss: 0.19329559803009033 acc:0.8\n",
      "Train step: 61000, loss: 0.15011057257652283 acc:0.85\n",
      "Train step: 61500, loss: 0.10000370442867279 acc:0.9\n",
      "Train step: 62000, loss: 0.149997740983963 acc:0.85\n",
      "Train step: 62500, loss: 0.20072512328624725 acc:0.8\n",
      "Train step: 63000, loss: 0.051551759243011475 acc:0.95\n",
      "Train step: 63500, loss: 0.25026363134384155 acc:0.75\n",
      "Train step: 64000, loss: 0.05057065933942795 acc:0.95\n",
      "Train step: 64500, loss: 0.2006661593914032 acc:0.8\n",
      "Train step: 65000, loss: 0.050267063081264496 acc:0.95\n",
      "---------------0.8130000000000003\n",
      "Train step: 65500, loss: 0.09994466602802277 acc:0.9\n",
      "Train step: 66000, loss: 0.20363548398017883 acc:0.8\n",
      "Train step: 66500, loss: 0.25017207860946655 acc:0.75\n",
      "Train step: 67000, loss: 0.25097745656967163 acc:0.75\n",
      "Train step: 67500, loss: 0.20055416226387024 acc:0.8\n",
      "Train step: 68000, loss: 0.1533479392528534 acc:0.85\n",
      "Train step: 68500, loss: 0.25335949659347534 acc:0.75\n",
      "Train step: 69000, loss: 0.05238572508096695 acc:0.95\n",
      "Train step: 69500, loss: 0.05354607105255127 acc:0.95\n",
      "Train step: 70000, loss: 0.12840530276298523 acc:0.85\n",
      "---------------0.8160000000000001\n",
      "Train step: 70500, loss: 0.10170348733663559 acc:0.9\n",
      "Train step: 71000, loss: 0.300295889377594 acc:0.7\n",
      "Train step: 71500, loss: 0.15064534544944763 acc:0.85\n",
      "Train step: 72000, loss: 0.10047303140163422 acc:0.9\n",
      "Train step: 72500, loss: 0.10077393054962158 acc:0.9\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train step: 73000, loss: 0.10112027823925018 acc:0.9\n",
      "Train step: 73500, loss: 0.20049166679382324 acc:0.8\n",
      "Train step: 74000, loss: 0.1498546451330185 acc:0.85\n",
      "Train step: 74500, loss: 0.10003991425037384 acc:0.9\n",
      "Train step: 75000, loss: 0.10000161081552505 acc:0.9\n",
      "---------------0.816\n",
      "Train step: 75500, loss: 0.05039341002702713 acc:0.95\n",
      "Train step: 76000, loss: 0.01451380830258131 acc:0.95\n",
      "Train step: 76500, loss: 1.5655963352401159e-06 acc:1.0\n",
      "Train step: 77000, loss: 4.051535597682232e-06 acc:1.0\n",
      "Train step: 77500, loss: 0.25000473856925964 acc:0.75\n",
      "Train step: 78000, loss: 0.20008771121501923 acc:0.8\n",
      "Train step: 78500, loss: 0.1500028520822525 acc:0.85\n",
      "Train step: 79000, loss: 0.1501830518245697 acc:0.85\n",
      "Train step: 79500, loss: 0.15043778717517853 acc:0.85\n",
      "Train step: 80000, loss: 0.14066478610038757 acc:0.85\n",
      "---------------0.8180000000000001\n",
      "Train step: 80500, loss: 0.15131019055843353 acc:0.85\n",
      "Train step: 81000, loss: 0.10000000149011612 acc:0.9\n",
      "Train step: 81500, loss: 0.1008254736661911 acc:0.9\n",
      "Train step: 82000, loss: 0.05000978708267212 acc:0.95\n",
      "Train step: 82500, loss: 0.15007467567920685 acc:0.85\n",
      "Train step: 83000, loss: 0.05000537633895874 acc:0.95\n",
      "Train step: 83500, loss: 0.04656556621193886 acc:0.95\n",
      "Train step: 84000, loss: 0.05088887736201286 acc:0.95\n",
      "Train step: 84500, loss: 0.05010455101728439 acc:0.95\n",
      "Train step: 85000, loss: 0.050004743039608 acc:0.95\n",
      "---------------0.8169999999999998\n",
      "Train step: 85500, loss: 0.1636827141046524 acc:0.8\n",
      "Train step: 86000, loss: 0.10030798614025116 acc:0.9\n",
      "Train step: 86500, loss: 0.10571974515914917 acc:0.9\n",
      "Train step: 87000, loss: 0.10031060129404068 acc:0.9\n",
      "Train step: 87500, loss: 0.21225640177726746 acc:0.8\n",
      "Train step: 88000, loss: 0.13911351561546326 acc:0.85\n",
      "Train step: 88500, loss: 0.10599557310342789 acc:0.9\n",
      "Train step: 89000, loss: 0.15024319291114807 acc:0.85\n",
      "Train step: 89500, loss: 0.3498730957508087 acc:0.65\n",
      "Train step: 90000, loss: 0.1528957635164261 acc:0.85\n",
      "---------------0.8155\n",
      "Train step: 90500, loss: 0.10968764126300812 acc:0.9\n",
      "Train step: 91000, loss: 0.050008583813905716 acc:0.95\n",
      "Train step: 91500, loss: 0.10054371505975723 acc:0.9\n",
      "Train step: 92000, loss: 0.20004062354564667 acc:0.8\n",
      "Train step: 92500, loss: 0.15000495314598083 acc:0.85\n",
      "Train step: 93000, loss: 0.2499973475933075 acc:0.75\n",
      "Train step: 93500, loss: 0.0002046009321929887 acc:1.0\n",
      "Train step: 94000, loss: 0.05017247796058655 acc:0.95\n",
      "Train step: 94500, loss: 0.2516276240348816 acc:0.75\n",
      "Train step: 95000, loss: 4.148686275584623e-05 acc:1.0\n",
      "---------------0.8190000000000001\n",
      "Train step: 95500, loss: 0.2004939615726471 acc:0.8\n",
      "Train step: 96000, loss: 0.001788630848750472 acc:1.0\n",
      "Train step: 96500, loss: 0.21176867187023163 acc:0.8\n",
      "Train step: 97000, loss: 0.1497015506029129 acc:0.85\n",
      "Train step: 97500, loss: 0.1017070859670639 acc:0.9\n",
      "Train step: 98000, loss: 0.007431505713611841 acc:1.0\n",
      "Train step: 98500, loss: 0.05009845644235611 acc:0.95\n",
      "Train step: 99000, loss: 0.2500019073486328 acc:0.75\n",
      "Train step: 99500, loss: 0.15082353353500366 acc:0.85\n"
     ]
    }
   ],
   "source": [
    "init = tf.global_variables_initializer()\n",
    "batch_size = 20\n",
    "train_steps = 100000\n",
    "test_steps = 100\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for i in range(train_steps):\n",
    "        batch_data,batch_labels = train_data.next_batch(batch_size)\n",
    "        loss_val,acc_val,_ = sess.run([loss,accuracy,train_op],feed_dict={x:batch_data,y:batch_labels})\n",
    "        \n",
    "        if i %500 == 0:\n",
    "            print(\"Train step: {}, loss: {} acc:{}\".format(i,loss_val,acc_val))\n",
    "            \n",
    "        if i %5000 == 0:\n",
    "            test_data = CifarData(test_filenames, False)\n",
    "            all_test_acc_val = []\n",
    "            for j in range(test_steps):\n",
    "                test_batch_data,test_batch_label = test_data.next_batch(batch_size)\n",
    "                test_acc_val = sess.run([accuracy], feed_dict = {x:test_batch_data,y:test_batch_label})\n",
    "                all_test_acc_val.append(test_acc_val)\n",
    "            \n",
    "            test_acc = np.mean(all_test_acc_val)\n",
    "            print(\"---------------{}\".format(test_acc))\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
